package com.example.hello_app.model;

import java.util.Objects;

// 响应类
// 会自动转化成JSON格式
public class AppResponse<T> {

    static public final int SUCCESS = 200;
    static public final int FAIL = 400;
    static public final int ERROR = 500;

    // success
    public static <T> AppResponse<T> success(T data) {
        AppResponse<T> appResponse = new AppResponse<>();
        appResponse.setCode(SUCCESS);
        appResponse.setMessage("success");
        appResponse.setData(data);
        return appResponse;
    }

    // fail
    public static AppResponse<Void> fail(String message) {
        AppResponse<Void> appResponse = new AppResponse<>();
        appResponse.setCode(FAIL);
        appResponse.setMessage(message);
        return appResponse;
    }

    // error
    public static AppResponse<Void> error(String message) {
        AppResponse<Void> appResponse = new AppResponse<>();
        appResponse.setCode(ERROR);
        appResponse.setMessage(message);
        return appResponse;
    }

    // message
    private String message;

    // code
    private int code;

    // data
    private T data;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        AppResponse<?> appResponse = (AppResponse<?>) o;
        return code == appResponse.code && Objects.equals(message, appResponse.message) && Objects.equals(data, appResponse.data);
    }

    @Override
    public int hashCode() {
        return Objects.hash(message, code, data);
    }

    @Override
    public String toString() {
        return "Response{" +
                "message='" + message + '\'' +
                ", code=" + code +
                ", data=" + data +
                '}';
    }
}
